from typing import List

from pydantic import BaseModel, validator, Field


# headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
#     'Accept-Encoding': 'gzip, deflate',
#     'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
#     'Host': 'kns.cnki.net',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
# }


class SaveStepInfoMongoModel(BaseModel):
    id: str = Field("", alias="_id")
    task_name: str = ""  # 数据来源标识 cnki
    types: str = ""  # qk
    stat: str = "0"
    tags: str = ""
    step_info: dict = dict()  # 步骤信息
    update_time: str = ""
    create_time: str = ""

    @validator('*', pre=True)
    def deal_none(cls, v, field):
        if v is None or v == {} or v == "":
            return field.default
        return v

    @validator('update_time', 'create_time', pre=True)
    def split_str(cls, v):
        return str(v)


class SaveStepLogMongoModel(BaseModel):
    id: str = Field("", alias="_id")
    task_name: str = ""  # 数据来源标识 cnki
    types: str = ""  # qk
    step_name: str = ""  # 步骤名g_jid
    stat: str = "0"
    tags: str = ""
    log_info: List[dict] = []  # 步骤信息
    update_time: str = ""
    create_time: str = ""

    @validator('*', pre=True)
    def deal_none(cls, v, field):
        if v is None or v == {} or v == "":
            return field.default
        return v

    @validator('update_time', 'create_time', pre=True)
    def split_str(cls, v):
        return str(v)


class StepModel(BaseModel):
    task_name: str = ""
    types: str = ""
    step_name: str = ""
    step_info_id: str = ""
    step_log_id: str = ""
    step_info_stat: str = "0"
    step_log_stat: str = "0"
    tags: str = ""
    step_log_info: dict = dict()


class OneRule(BaseModel):
    key: str = ""  # 对报告的哪个key进行验证
    rule: str = ""
    value: str = ""


class VerifyRuleRequestModel(BaseModel):
    task_name: str = ""
    types: str = ""
    step_name: str = ""
    tags: str = ""
    retry: str = "0"
    is_skip: str = "false"
    keys: List[OneRule] = []


class VerifyRuleModel(BaseModel):
    id: str = Field("", alias="_id")
    task_name: str = ""
    types: str = ""
    step_name: str = ""
    tags: str = ""
    retry: str = "0"
    is_skip: str = ""
    keys: List[OneRule] = []
    update_time: str = ""
    create_time: str = ""

    @validator('update_time', 'create_time', pre=True)
    def split_str(cls, v):
        return str(v)


class UseRule(BaseModel):
    data_log: dict = {}
    rule_list: List[OneRule] = []


class UpdateLogModel(BaseModel):
    id: str = ""
    stat: str = ""
    step_log_info: dict = dict()


class ProcessModel(BaseModel):
    task_name: str = None
    types: str = None
    step_name: str = None  # 查询info表时没有该值
    tags: str = None
    id: str = Field(None, alias="_id")
    stat: str = None


class StepInfoModel(BaseModel):
    stat: str = None
    step_info: dict = {}


class LogInfoModel(BaseModel):
    id: str = ""
    is_all_client = "false"  # 是否全量替换 client_data
    client_data: dict = {}
    is_all_rule = "false"  # 是否全量替换 rule_data
    rule_data: dict = {}


class StepLogModel(BaseModel):
    stat: str
    log_info: LogInfoModel


class StepRuleModel(BaseModel):
    retry: str = "0"
    is_skip: str = "false"
    keys: List[OneRule]


class UpInInfoModel(BaseModel):
    query: ProcessModel
    update: StepInfoModel


class UpInLogModel(BaseModel):
    query: ProcessModel
    update: StepLogModel


class UpInRuleModel(BaseModel):
    query: ProcessModel
    update: StepRuleModel
